Skip to content

HTTP GET请求 - HttpGet

函数简介

发送简单的HTTP GET请求,返回响应体字符串。

接口名称

HttpGet

DLL调用

c
const char* HttpGet(int64_t instance, const char* url);

参数说明

参数名类型说明
instance长整数型OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。
url字符串完整URL。

使用场景

场景示例
拉取 REST API 数据HttpGet("https://api.example.com/v1/items?page=1")
获取网页/HTML 内容HttpGet("https://example.com/status")
下载小文本/JSON响应体直接作为字符串返回

查询参数需 直接拼在 URL 中;如需自定义 Header 或 Cookie,请改用 HttpRequestEx

示例

SDK 调用

cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
// GET 请求,查询参数直接拼在 URL 中
string url = "https://httpbin.org/get?user=ola&page=1";
string body = ola.HttpGet(url);
if (!body.empty()) {
    // body 为响应 JSON,可用 JsonParse 解析
}
csharp
using OLAPlug;

var ola = new OLAPlugServer();
// GET 请求,查询参数直接拼在 URL 中
string url = "https://httpbin.org/get?user=ola&page=1";
string body = ola.HttpGet(url);
if (!string.IsNullOrEmpty(body))
{
    // body 为响应 JSON
}
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
url = "https://httpbin.org/get?user=ola&page=1"
body = ola.HttpGet(url)
if body:
    # body 为响应 JSON
    pass
java
import com.olaplug.OLAPlugServer;

OLAPlugServer ola = new OLAPlugServer();
String url = "https://httpbin.org/get?user=ola&page=1";
String body = ola.HttpGet(url);
if (body != null && !body.isEmpty()) {
    // body 为响应 JSON
}
go
import "github.com/ola/olaplug/olaplug"

ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
url := "https://httpbin.org/get?user=ola&page=1"
body := ola.HttpGet(url)
// body 为响应正文,失败时可能为空
rust
use olaplug::OLAPlugServer;

let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
let body = ola.http_get("https://httpbin.org/get?user=ola&page=1");
// body 为响应正文
cpp
var ola = com("OlaPlug.OlaSoft")
var url = "https://httpbin.org/get?user=ola&page=1"
var body = ola.HttpGet(url)
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
url = "https://httpbin.org/get?user=ola&page=1"
body = ola.HttpGet(url)
text
.局部变量 ola, OLAPlug
ola.创建 ()
url = "https://httpbin.org/get?user=ola&page=1"
body = ola.HttpGet (url)
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var url = "https://httpbin.org/get?user=ola&page=1";
var body = ola.HttpGet(url);
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
文本型 url = "https://httpbin.org/get?user=ola&page=1"
文本型 body = ola.HttpGet(url)
cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
string url = "https://httpbin.org/get?user=ola&page=1";
string body = ola.HttpGet(url);

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
long bodyPtr = HttpGet(instance, "https://httpbin.org/get?user=ola&page=1");
if (bodyPtr != 0) {
    char body[4096] = {0};
    GetStringFromPtr(bodyPtr, body, sizeof(body));
    FreeStringPtr(bodyPtr);
}
csharp
using System.Runtime.InteropServices;
using System.Text;

[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long HttpGet(long ola, string url);

long instance = CreateCOLAPlugInterFace();
long bodyPtr = HttpGet(instance, "https://httpbin.org/get?user=ola&page=1");
if (bodyPtr != 0) {
    StringBuilder body = new StringBuilder(GetStringSize(bodyPtr) + 1);
    GetStringFromPtr(bodyPtr, body, body.Capacity);
    FreeStringPtr(bodyPtr);
}
python
from ctypes import CDLL, c_int, c_int64, create_string_buffer

ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
bodyPtr = ola.HttpGet(instance, b"https://httpbin.org/get?user=ola&page=1")
if bodyPtr:
    buf = create_string_buffer(4096)
    ola.GetStringFromPtr(bodyPtr, buf, 4096)
    ola.FreeStringPtr(bodyPtr)
    body = buf.value.decode("utf-8")

返回值

返回值说明
(返回值)字符串指针,返回响应体内容,失败返回NULL。需调用 FreeStringPtr 释放内存。

注意事项

项目说明
释放内存返回的字符串需要调用 FreeStringPtr 释放内存。
适合简单的GET请求适合简单的GET请求。
如需自定义请求头或Cookie如需自定义请求头或Cookie,请使用 HttpRequestEx
支持HTTP和HTTPS协议支持HTTP和HTTPS协议。